datadir=
ramdisk_size=
datadir_min=0
datadir_max=0
os_version=`uname -r | grep 2.6.9` 

function getdisksize()
{ 
  dfinfo=`df $1`
  lang=`echo $LANG | grep "zh_CN"`
  if [ "$lang" != "" ]
  then
    tmp=`echo $dfinfo | awk '{print $8}'`
  else   
    tmp=`echo $dfinfo | awk '{print $9}'`
  fi     
  
  if [ $datadir_min -eq 0 -o $tmp -lt $datadir_min ]
  then  
    datadir_min=$tmp
  fi  
  if [ $datadir_min -eq 0 -o $tmp -gt $datadir_max ]
  then  
    datadir_max=$tmp
  fi  
} 
if [ ! -d /opt/.uninstall ]
then
#	echo "mkdir recover"
	mkdir /opt/.uninstall
fi

cp /etc/bashrc /opt/.uninstall/bashrc

cp /boot/grub/grub.conf /opt/.uninstall/grub.conf

#	echo "save /etc/rc.local for recover"
cp /etc/rc.local /opt/.uninstall/rc.local


groupadd mysql
useradd -g mysql mysql

rm -fr /tmp/ramdisk
mkdir /tmp/ramdisk
chmod 777 /tmp/ramdisk
echo "Please input Mysql datadir[default datadir=/home/mysql]:"
read datadir
if [ -z $datadir ]
then
	datadir=/home/mysql
	if [ ! -d "$datadir" ]; then
  mkdir /home/mysql
  fi 
else 
	if [ ! -d "$datadir" ]; then
		echo "$mountdir does not exists, install will rollback! "  
		/bin/sh /opt/uninstall.sh
		exit 1
	fi  
fi
echo "The Mysql datadir=$datadir"
###################

touch t.txt
chmod 777 t.txt

getdisksize $datadir
################################


rm -rf ${datadir}/*

function createFold()
{
	mkdir -p $1/DATA/
	mkdir -p $1/DATA/NPE_RECORD
	mkdir  $1/DATA/NPE_DEVICE_TRAFFIC 
	mkdir  $1/DATA/NPE_SOURCE_TRAFFIC  
	mkdir  $1/DATA/NPE_NAT_DEST_TRAFFIC  
	mkdir  $1/DATA/USER_TRAFFIC   
	mkdir  $1/DATA/RGWALL_NAT
	mkdir  $1/DATA/RGWALL_URL  
	mkdir  $1/DATA/ACE_URL  
	mkdir  $1/DATA/APP_TRAFFIC
	mkdir  $1/DATA/ONLINE_DETAIL  
	mkdir  $1/DATA/DAY_AGGR 	
	mkdir  $1/DATA/IPFIX
	mkdir  $1/DATA/TOS_TRAFFIC
	mkdir	 $1/DATA/TCP_FLAG_TRAFFIC
	mkdir	 $1/DATA/IM
	#mkdir	 $1/DATA/DXT_NAT
	#mkdir	 $1/DATA/PANABIT_URL
	mkdir  $1/DATA/RGEG_IM
 mkdir  $1/DATA/RGEG_MAIL
 mkdir  $1/DATA/RGEG_WEBBBS
 mkdir  $1/DATA/RGEG_URL
 mkdir  $1/DATA/RGEG_NAT
	
	x=0
	while [ ${x} -le  90 ]; do
		mkdir $1/DATA/NPE_RECORD/${x}
		mkdir $1/DATA/RGWALL_URL/${x}  
		mkdir $1/DATA/RGWALL_NAT/${x}   
		mkdir $1/DATA/ACE_URL/${x} 
		mkdir $1/DATA/IPFIX/${x}	  
		mkdir $1/DATA/IM/${x}
		#mkdir $1/DATA/DXT_NAT/${x}
		#mkdir	$1/DATA/PANABIT_URL/${x}
		mkdir	$1/DATA/RGEG_IM/${x}
	 mkdir	$1/DATA/RGEG_MAIL/${x}
	 mkdir	$1/DATA/RGEG_WEBBBS/${x}
	 mkdir	$1/DATA/RGEG_URL/${x}
	 mkdir	$1/DATA/RGEG_NAT/${x}
		let x=x+1
	done;	
	
	chmod 777 -R $1
}

createFold ${datadir}

dtfamat=$(echo ${datadir//\//\\\/})
sed "s/INSERT INTO \`DISK_CONFIG\` VALUES(0, '\/home\/mysql');/INSERT INTO \`DISK_CONFIG\` VALUES(0, '$dtfamat');/" /opt/init.sql > init.sql.tmp
mv init.sql.tmp /opt/init.sql

quit=0
i=1
mountdir=
until [ $quit -eq 1 ]
do
	echo -n "Please Input the other disk mount path(press ENTER key to out): "
	read mountdir
	if [ -z $mountdir ]
	then
		quit=1
	else
				if [ ! -d "$mountdir" ]
				then
						echo "$mountdir does not exists, install will rollback! "  
				   /bin/sh /opt/uninstall.sh
   					exit 1 
		    fi
        getdisksize ${mountdir}
################################
	
		if [ ! -d ${mountdir} ] 
		then
			mkdir ${mountdir} 
		else
			rm -rf ${mountdir}/*
		fi 
		
		createFold ${mountdir}

		mtfamat=$(echo ${mountdir//\//\\\/})
		sed -i "/INSERT INTO \`DISK_CONFIG\` VALUES(0, '$dtfamat');/ i INSERT INTO \`DISK_CONFIG\` VALUES($i, '$mtfamat');" /opt/init.sql
		i=$(expr $i + 1)
	fi
done
#####################

let c=100*$datadir_min/$datadir_max
if [ ${c} -ge 80 ];then 
   echo "disk space check success !"
else
   echo "Install failed! Data dir must be same size!"
   /bin/sh /opt/uninstall.sh
   exit 1 
fi
#####################


chmod +x -R /opt/*
#echo "Init ramdisk... Please input the ramdisk size(K)"
#read ramdisk_size
#if [ -z $ramdisk_size ]
#then
#	echo "Use default 200M"
#	ramdisk_size=200000
#fi

#echo "The ramdisk is set to $ramdisk_size"
echo "*                soft    nofile          50000">> /etc/security/limits.conf
echo "*                hard    nofile          65536 ">>  /etc/security/limits.conf

echo "begin to init mysql configure file.........."

echo "# The following options will be passed to all MySQL clients" > /opt/mysql/my.cnf
echo "[client]" >> /opt/mysql/my.cnf
echo "port            = 3306" >> /opt/mysql/my.cnf
echo "socket          = /tmp/mysql.sock" >> /opt/mysql/my.cnf

echo "[mysqld]" >> /opt/mysql/my.cnf
echo "port            = 3306" >> /opt/mysql/my.cnf
echo "socket          = /tmp/mysql.sock" >> /opt/mysql/my.cnf
echo "default-character-set = utf8" >> /opt/mysql/my.cnf
echo "skip-locking" >> /opt/mysql/my.cnf
echo "key_buffer = 384M" >> /opt/mysql/my.cnf
echo "max_allowed_packet = 1M" >> /opt/mysql/my.cnf
echo "table_cache = 256" >> /opt/mysql/my.cnf
echo "sort_buffer_size = 2M" >> /opt/mysql/my.cnf
echo "read_buffer_size = 2M" >> /opt/mysql/my.cnf
echo "read_rnd_buffer_size = 8M" >> /opt/mysql/my.cnf 
echo "tmp_table_size = 32M" >>  /opt/mysql/my.cnf 
echo "max_heap_table_size = 32M"  >>  /opt/mysql/my.cnf 
echo "net_buffer_length = 2K" >> /opt/mysql/my.cnf
echo "thread_stack = 64K" >> /opt/mysql/my.cnf
echo "myisam_sort_buffer_size = 800M" >> /opt/mysql/my.cnf
echo "datadir=$datadir" >> /opt/mysql/my.cnf
echo "server-id		=1" >> /opt/mysql/my.cnf
echo "[mysqldump]" >> /opt/mysql/my.cnf
echo "quick" >> /opt/mysql/my.cnf
echo "max_allowed_packet = 16M" >> /opt/mysql/my.cnf
#echo "open_files_limit = 50000" >> /opt/mysql/my.cnf  
echo "[mysql]" >> /opt/mysql/my.cnf
echo "no-auto-rehash" >> /opt/mysql/my.cnf
echo "default-character-set = utf8" >> /opt/mysql/my.cnf
#default-character-set=gbk
#add by chenhy 2008-4-30 end
#temp add by chenhy 2008-5-7 begin test: load linux txt for mqtxt  Òª¼ÇµÃÆÁ±Îµô
#binlog-do-db=UAS001
#temp add by chenhy 2008-5-7
# Remove the next comment character if you are not familiar with SQL
#safe-updates
 
echo "[isamchk]" >> /opt/mysql/my.cnf
echo "key_buffer = 8M" >> /opt/mysql/my.cnf
echo "sort_buffer_size = 8M" >> /opt/mysql/my.cnf
 
echo "[myisamchk]" >> /opt/mysql/my.cnf
echo "key_buffer = 8M" >> /opt/mysql/my.cnf
echo "sort_buffer_size = 8M" >> /opt/mysql/my.cnf
 
echo "[mysqlhotcopy]" >> /opt/mysql/my.cnf
echo "interactive-timeout" >> /opt/mysql/my.cnf

#=============================
echo "export JAVA_HOME=/opt/j2sdk" >> /etc/bashrc
echo "export MYSQL_HOME=/opt/mysql" >> /etc/bashrc
source /etc/bashrc


echo "begin to install mysql database........." 
chmod +x /opt/mysql/bin/*
chmod +x  /opt/mysql/libexec/*
/opt/mysql/bin/mysql_install_db --user=mysql

echo "begin to init mysql table..............."
/opt/mysql/bin/mysqld_safe & sleep 1

testtime=1
while [ ${testtime} -le 5 ]; do
   count=`ps -ef | grep [m]ysql | wc -l`
   if [ $count -gt 1 ]; then
	break
   else
        echo "mysql service is starting"
   fi
 
   sleep 5
   let testtime=testtime+1
done;

count=`ps -ef | grep [m]ysql | wc -l`
if [ $count -gt 1 ]; then
    echo "mysql service start success!"
else
    echo "mysql service start fail,a rollback will be performed!"
    source /opt/uninstall.sh
    exit 1
fi

/opt/mysql/bin/mysql < /opt/init.sql

/opt/mysql/bin/mysql -e "grant all privileges on *.* to 'mysql'@'localhost' identified by 'mysql';"

echo "begin to set mysql to auto run..........."
cp /opt/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql 
cd /etc/init.d/
chkconfig --add mysql

echo "begin to generate a new excutable logServer......."
cd /opt/npeserver
make
rm -f *.c *.h

myFile="/opt/npeserver/npeserver"
if [ ! -f "$myFile" ]; then
 echo "npeserver installation failed,a rollback will be performed!"
 source /opt/uninstall.sh
 exit 1
fi

#sed "s/\([ ,\t]*kernel.*\)/\1 ramdisk_size=$ramdisk_size/" /boot/grub/grub.conf > grub.conf.tmp
#mv grub.conf.tmp /boot/grub/grub.conf

echo "..............................."
echo "export JAVA_HOME=/opt/j2sdk" > /etc/rc.local
echo "export LANG=zh_CN.gbk" >> /etc/rc.local
if [ -z ${os_version} ]
then
	echo "/opt/dogdriver/centos5.x/usbdaemon" >> /etc/rc.local
else
		kernel_version=`uname -r | grep smp`	
	  if [ -z ${kernel_version} ]
		then
        cd /opt/dogdriver/centos4.6/El
        make install
        echo "install dog driver succ!"
		else
        cd /opt/dogdriver/centos4.6/Elsmp
        make install
        echo "install dog deriver for SMP SUCC!"
		fi
fi
echo "/opt/jboss/bin/run.sh &" >> /etc/rc.local

#echo "mke2fs /dev/ram0">> /etc/rc.local
#echo "mount /dev/ram0 /opt/npeserver/ramdisk">>/etc/rc.local

echo "sysctl -w net.core.rmem_max=8388608" >> /etc/rc.local
echo "sysctl -w net.core.wmem_max=8388608" >> /etc/rc.local
echo "sysctl -w net.core.rmem_default=8388608" >> /etc/rc.local
echo "sysctl -w net.core.wmem_default=8388608" >> /etc/rc.local

echo "ulimit -c 1000000" >> /etc/rc.local
echo "/opt/j2sdk/bin/java -jar /opt/jms/jms.jar &" >> /etc/rc.local

echo "#Now begin to Start receiving NPE Log packet.............." >>/etc/rc.local
echo "cd /opt/npeserver">> /etc/rc.local
echo "./npeserver &" >> /etc/rc.local
echo "touch /var/lock/subsys/local" >> /etc/rc.local

echo "Setup NTP server..."
if rpm -q ntp>/dev/null
then
	echo "NTP has been installed, Configure..."
else
	echo "begin to install NTP server..."

	cd /opt/
	rpm -ivh ntp-4.2.0.a.20040617-6.el4.i386.rpm
fi

if [ ! -f /etc/ntp.conf ]
then
	echo "NTP server installation failed,a rollback will be performed."
	source /opt/uninstall.sh
	exit 1
else
	sed "s/restrict default nomodify notrap noquery/restrict default nomodify notrap/" /etc/ntp.conf > /etc/ntp.conf.tmp
	mv /etc/ntp.conf.tmp /etc/ntp.conf
fi

chkconfig ntpd on
if [ ! -f /etc/init.d/ntpd ]
then
	echo "service ntpd start >> /etc/rc.local"
else
	chkconfig --add ntpd
fi

if rpm -q aksusbd>/dev/null
then
	echo "aksusbd has been installed..."
else
	echo "begin to install aksusbd..."

	cd /opt/
	rpm -ivh aksusbd-1.15-1.i386.rpm
fi

if rpm -q aksusbd>/dev/null
then
	echo "aksusbd installed success..."
else
	echo "aksusbd installation failed,a rollback will be performed."
	source /opt/uninstall.sh
	exit 1
fi
# ================end==================================

#--------------------------- 

if [ ! -d /opt/.recover ]
then
#	echo "mkdir recover"
	mkdir /opt/.recover
fi

cp /etc/bashrc /opt/.recover/bashrc

cp /boot/grub/grub.conf /opt/.recover/grub.conf

#	echo "save /etc/rc.local for recover"
cp /etc/rc.local /opt/.recover/rc.local

#####################
#rpm -ivh --force /opt/microdog-4.0.0-1.i386.rpm

if [ -z ${os_version} ]
then
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libMhlinuxj.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libMhlinuxj.so /opt/.recover/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libHASPJava.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libHASPJava.so /opt/.recover/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libhasp_linux_96224.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos5.x/libhasp_linux_96224.so /opt/.recover/j2sdk/jre/lib/i386/
else
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libMhlinuxj.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libMhlinuxj.so /opt/.recover/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libHASPJava.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libHASPJava.so /opt/.recover/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libhasp_linux_96224.so /opt/j2sdk/jre/lib/i386/
		/bin/cp -f /opt/libmhlinuxj/centos4.6/libhasp_linux_96224.so /opt/.recover/j2sdk/jre/lib/i386/
fi		
#######################

echo "Save needed file for recover"
cp -r /opt/conf /opt/.recover/
cp -r /opt/dogdriver /opt/.recover/
cp /opt/init.sql  	/opt/.recover/
cp /opt/install.sh 	/opt/.recover/
cp -r /opt/j2sdk		/opt/.recover/
cp -r /opt/jboss		/opt/.recover/
cp -r /opt/jms			/opt/.recover/
cp -r /opt/lib-jms-client       /opt/.recover/
cp -r /opt/npeserver		/opt/.recover/


echo "Install finished, Please reboot your computer"